package com.corpgovernment.organization.controller.budget;

import com.corpgovernment.common.base.BaseUserInfo;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.organization.enumm.budget.BudgetOperationEnum;
import com.corpgovernment.organization.service.budget.AddBudgetService;
import com.corpgovernment.organization.service.budget.ChangeBudgetService;
import com.corpgovernment.organization.service.budget.SearchBudgetService;
import com.corpgovernment.organization.vo.budget.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/travelBudget")
public class BudgetController {

	@Autowired
	private SearchBudgetService searchBudgetService;
	@Autowired
	private AddBudgetService addBudgetService;
	@Autowired
	private ChangeBudgetService changeBudgetService;

	/**
	 * 搜索部门接口
	 */
	@RequestMapping(value = "/searchDepart", method = RequestMethod.POST)
	public JSONResult searchDepart(@RequestBody @Valid SearchDepartVo searchDepartVo) {
		return JSONResult.success(searchBudgetService.searchDepart(searchDepartVo.getCorpId(), searchDepartVo.getSearchName()));
	}

	/**
	 * 预算设置页查询接口
	 */
	@RequestMapping(value = "/searchBudget", method = RequestMethod.POST)
	public JSONResult searchBudget(@RequestBody @Valid SearchBudgetVo searchBudgetVo) {
		return JSONResult.success(searchBudgetService.searchBudget(searchBudgetVo));
	}

	/**
	 * 预算查看
	 */
	@RequestMapping(value = "/seeBudget", method = RequestMethod.POST)
	public JSONResult seeBudget(@RequestBody @Valid SeeBudgetVo seeBudgetVo) {
		return JSONResult.success(searchBudgetService.seeBudget(seeBudgetVo));
	}

	/**
	 * 停用/启用接口
	 */
	@RequestMapping(value = "/revStop", method = RequestMethod.POST)
	public JSONResult revStop(@RequestBody @Valid RevStopVo revStopVo) {
		changeBudgetService.revStop(revStopVo.getId(), revStopVo.getStatus());
		return JSONResult.ok();
	}

	/**
	 * 删除接口
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public JSONResult delete(@RequestBody @Valid DeleteVo deleteVo) {
		return JSONResult.success(changeBudgetService.deleteBudget(deleteVo.getId()));
	}

	/**
	 * 按部门/按成本中心切换校验是否冲突
	 */
	@RequestMapping(value = "/switch", method = RequestMethod.POST)
	public JSONResult switchDepCos(@RequestBody @Valid SwitchVo switchVo) {
		return JSONResult.success(changeBudgetService.switchDepCos(switchVo));
	}

	/**
	 * 点击新增预算配置查询树
	 */
	@RequestMapping(value = "/searchTree", method = RequestMethod.POST)
	public JSONResult searchTree(@RequestParam(value = "corpId", required = false) String corpId) {
		return JSONResult.success(changeBudgetService.searchBudgetTypes());
	}

	/**
	 * 新增/追加编辑预算
	 */
	@RequestMapping(value = "/addBudget", method = RequestMethod.POST)
	public JSONResult addBudget(@RequestBody @Valid AddBudgetVo addBudgetVo, BaseUserInfo userInfo) {

		AddBudgetVo oldAddBudgetVo = null;
		if (BudgetOperationEnum.UPDATE.getCode().equals(addBudgetVo.getType())) {
			oldAddBudgetVo = searchBudgetService.getBudgetVo(addBudgetVo.getId());
		}
		addBudgetService.addOrUpdateBudget(addBudgetVo, userInfo);
		//增加操作日志
		if (BudgetOperationEnum.UPDATE.getCode().equals(addBudgetVo.getType())) {
			AddBudgetVo newAddBudgetVo = searchBudgetService.getBudgetVo(addBudgetVo.getId());
			addBudgetService.insertOperatorLog(oldAddBudgetVo, newAddBudgetVo);
		} else if (BudgetOperationEnum.ADD.getCode().equals(addBudgetVo.getType())) {
			addBudgetService.insertOperatorLog(null, addBudgetVo);
		}
		return JSONResult.ok();
	}

	/**
	 * 点击追加数据回显
	 */
	@RequestMapping(value = "/clickAdd", method = RequestMethod.POST)
	public JSONResult clickAdd(@RequestBody @Valid ClickAddVo clickAddVo) {
		return JSONResult.success(searchBudgetService.clickAdd(clickAddVo.getId()));
	}
}